//
//  InputAlipayAccountViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/30.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "InputAlipayAccountViewController.h"
#import "VerifyCodeButton.h"
#import "WithdrawDetailViewController.h"

@interface InputAlipayAccountViewController ()
/**
 姓名bg
 */
@property (nonatomic , strong) UIView *nameBgView;
/**
 账号 验证码bg
 */
@property (nonatomic , strong) UIView *accountBgView;
/**
 姓名
 */
@property (nonatomic , strong) YYLabel *nameLab;
/**
 只能本人绑定支付宝tips
 */
@property (nonatomic , strong) UILabel *tips;
/**
 横线
 */
@property (nonatomic , strong) UIView *hLine;
/**
 小竖线
 */
@property (nonatomic , strong) UIView *sLine;
/**
 z支付宝账号input
 */
@property (nonatomic , strong) UITextField *accountField;
/**
 验证码input
 */
@property (nonatomic , strong) UITextField *codeField;
/**
 验证码Btn
 */
@property (nonatomic , strong) VerifyCodeButton *codeBtn;
/**
 confirmBtn
 */
@property (nonatomic , strong) UIButton *confirmBtn;

/**
 memberId
 */
@property (nonatomic , strong) NSNumber *memberId;
@end

@implementation InputAlipayAccountViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现到支付宝";
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.nameBgView];
    [self.nameBgView addSubview:self.nameLab];
    [self.nameBgView addSubview:self.tips];
    
    [self.view addSubview:self.accountBgView];
    [self.accountBgView addSubview:self.accountField];
    [self.accountBgView addSubview:self.hLine];
    [self.accountBgView addSubview:self.codeField];
    [self.accountBgView addSubview:self.codeBtn];
    [self.accountBgView addSubview:self.sLine];
    [self.view addSubview:self.confirmBtn];
}

- (void) initLayout {
    [self.nameBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(50);
    }];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.nameBgView.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    [self.tips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLab.mas_right).offset(20);
        make.centerY.mas_equalTo(self.nameBgView.mas_centerY);
        make.height.mas_equalTo(16);
        make.width.mas_equalTo(109 * [JTool getScreenWidthScale]);
    }];
    [self.accountBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.nameBgView.mas_bottom).offset(10);
        make.height.mas_equalTo(101);
    }];
    [self.accountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(50);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
    }];
    [self.hLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.accountField.mas_bottom).offset(0);
        make.height.mas_equalTo(1);
    }];
    [self.codeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(150);
        make.top.mas_equalTo(self.hLine.mas_bottom);
    }];
    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.hLine.mas_bottom);
    }];
    [self.sLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.codeBtn.mas_left).offset(-15);
        make.width.mas_equalTo(1);
        make.top.mas_equalTo(self.hLine.mas_bottom).offset(7);
        make.bottom.mas_equalTo(-7);
    }];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.bottom.mas_equalTo(-60 * [JTool getScreenWidthScale]);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(48 * [JTool getScreenWidthScale]);
    }];
}

- (void) initAction {
    
    RACSignal *countDowningSignal = RACObserve(self.codeBtn, isCountDowning);
    RAC(self.codeBtn,enabled) = [RACSignal combineLatest:@[countDowningSignal] reduce:^id _Nonnull( NSNumber *countDowningState){
        return @(!countDowningState.boolValue);
    }];
    [self.codeBtn addTarget:self action:@selector(getCodeReauest) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)initData {
    //上次提现的支付宝记录
    [self withdrawAlipayHistoryRecordRequest];
}

#pragma mark - delegate

#pragma mark - target action
- (void)confirmBtnClick {
    NSString *account = self.accountField.text;
    NSString *code = self.codeField.text;
    if ([JTool iSNull:account]) {
        [self toastWithContent:@"请输入支付宝账号"];
        return;
    }
    if ([JTool iSNull:code]) {
        [self toastWithContent:@"请输入验证码"];
        return;
    }
    if ([JTool iSNull:self.codeField.text]) {
        [self toastWithContent:@"请输入验证码验证码"];
        return;
    }
    @weakify(self);
    [[PayInputView sharedInstance] display];
    [[PayInputView sharedInstance] setInputCompleteBlock:^(SecureTextEntryView * _Nonnull secureView) {
        @strongify(self);
        //提现到支付宝账号请求接口
        [self withdrawAlipayAccountRequest];
    }];
}
#pragma mark - private method
//上次提现的支付宝记录
- (void)withdrawAlipayHistoryRecordRequest {
    self.memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;

    @weakify(self);
    [[[PigMarketApi sharedInstance] getWithdrawAlipayAccoutnHistoryRecordWithMemberId:self.memberId] subscribeNext:^(WithdrawAlipayAccountHistoryRecord * _Nullable record) {
        @strongify(self);
        NSString *nameStr = [NSString stringWithFormat:@"姓名：%@",record.memberName];
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:nameStr];
        text.yy_font = [UIFont boldSystemFontOfSize:16];
        text.yy_color = [UIColor colorWithHexString:@"#333333"];
        NSRange range = NSMakeRange(0, 3);
        [text yy_setFont:[UIFont systemFontOfSize:16] range:range];
        [text yy_setColor:[UIColor colorWithHexString:@"#808080"] range:range];
        self.nameLab.attributedText = text;

        self.accountField.text = record.withdrawZfb;
        
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}
//获取验证码
- (void)getCodeReauest {
    
    [self.codeBtn startCountDown];
    GetMessageCodeParam *param = [[GetMessageCodeParam alloc] init];
    param.phone = [UserInfoDataProvider sharedInstance].userInfo.phone;
    param.type = GetMessageCodeTypeChangePassword;
    @weakify(self);
    [[[PigMarketApi sharedInstance] getMessageCodeWithParam:param] subscribeNext:^(GetMessageCode * _Nullable code) {
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
        [self.codeBtn stopCountDown];
    }];
}
//支付宝账户绑定进行提现操作（支付提现不走提现接口，直接走账户绑定接口）
- (void)withdrawAlipayAccountRequest {
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] alipayAccountWithMemberId:self.memberId account:self.accountField.text code:self.codeField.text money:self.withdrawMoney] subscribeNext:^(NSNull * _Nullable x) {
        @strongify(self);
        WithdrawDetailViewController *vc = [[WithdrawDetailViewController alloc] init];
        vc.withdrawMoney = self.withdrawMoney;
        vc.account = [NSString stringWithFormat:@"支付宝%@",@"账户号"];
        [[PayInputView sharedInstance] unDisplay];
        [self.navigationController pushViewController:vc animated:YES];
       
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
        [[PayInputView sharedInstance] unDisplay];
    }];
}

#pragma mark - getter and setter
- (UIView *)nameBgView {
    if (!_nameBgView) {
        _nameBgView = [[UIView alloc] init];
        _nameBgView.backgroundColor = [UIColor whiteColor];
    }
    return _nameBgView;
}
- (YYLabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [[YYLabel alloc] init];
     }
    return _nameLab;
}
- (UILabel *)tips {
    if (!_tips) {
        _tips = [[UILabel alloc] init];
        _tips.textColor = [UIColor colorWithHexString:@"#EC8C00"];
        _tips.font = [UIFont systemFontOfSize:11];
        _tips.backgroundColor = [UIColor colorWithHexString:@"#FFF9D4"];
        _tips.text = @"只能绑定本人支付宝";
        _tips.textAlignment = NSTextAlignmentCenter;
    }
    return _tips;
}
- (UIView *)accountBgView {
    if (!_accountBgView) {
        _accountBgView = [[UIView alloc] init];
        _accountBgView.backgroundColor = [UIColor whiteColor];
    }
    return _accountBgView;
}
- (UITextField *)accountField {
    if (!_accountField) {
        _accountField = [[UITextField alloc] init];
        _accountField.placeholder = @"请输入您的支付宝账号";
        [_accountField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    }
    return _accountField;
}
- (UIView *)hLine {
    if (!_hLine) {
        _hLine = [[UIView alloc] init];
        _hLine.backgroundColor = [UIColor colorWithHexString:@"#D8D8D8"];
    }
    return _hLine;
}
- (UIView *)sLine {
    if (!_sLine) {
        _sLine = [[UIView alloc] init];
        _sLine.backgroundColor = [UIColor colorWithHexString:@"#D8D8D8"];
    }
    return _sLine;
}
- (UITextField *)codeField {
    if (!_codeField) {
        _codeField = [[UITextField alloc] init];
        _codeField.placeholder = @"请输入验证码";
        [_codeField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        _codeField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _codeField;
}
- (VerifyCodeButton *)codeBtn {
    if (!_codeBtn) {
        _codeBtn = [VerifyCodeButton buttonWithType:UIButtonTypeCustom];
        _codeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        
    }
    return _codeBtn;
}
- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn_default"] forState:UIControlStateNormal];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    }
    return _confirmBtn;
}
@end
